package cn.itcast.dao.impl;

import cn.itcast.dao.ChangeDao;
import cn.itcast.domain.Change;
import cn.itcast.domain.User;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ChangeDaoImpl  implements ChangeDao {
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public void updateChangeInfo(int parseInt, int parseInt1, String info) {
        String sql="update `change` set info_a=?,info_b=?,state_id=?,change_state=?,check_state=? where id=? and yid=?";
        try {
            template.update(sql,info,"empty",1,0,0,parseInt,parseInt1);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Change> findAll() {
        //1定义sql
        String sql="select *from `change`";
        List<Change> changes=null;
        try {
            changes = template.query(sql, new BeanPropertyRowMapper<Change>(Change.class));
            return changes;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Change findChangeById(int parseInt) {
        String sql="select*from `change` where id = ?";
        Change change=null;
        try {
            change=template.queryForObject(sql,new BeanPropertyRowMapper<Change>(Change.class),parseInt);
            return change;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void updateChangeInfob(Change change) {
        String sql="update `change` set info_b=? where id=? ";
        try {
            template.update(sql,change.getInfo_b(),change.getId());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateChangeStateId(int id, int state_id) {
        String sql="update `change` set state_id=? ,change_state=0,check_state=0 where id = ?";
        try {
            template.update(sql,state_id,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateChangeChangeState(int id, int change_state) {
        String sql="update `change` set change_state=?,check_state=0 where id = ?";
        try {
            template.update(sql,change_state,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateChangeCheckState(int id, int check_state) {
        String sql ="update `change` set check_state=? where id=?";
        try {
            template.update(sql,check_state,id);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addChange(int id, String info_a, int yid) {
        String sql="insert into `change` values(?,?,?,'empty',1,?,0,0)";
        try {
            template.update(sql,id,id,info_a,yid);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }
}
